<?php

namespace Modules\Admin\Http\Controllers;

use App\Common\Lib\GoogleAuthenticator;
use App\Common\Lib\Validate;
use App\Services\AdminService;

class AuthController extends Controller
{
    protected $adminService;
    protected $google_auth;

    public function __construct(AdminService $adminService, GoogleAuthenticator $google_auth)
    {
        $this->adminService = $adminService;
        $this->google_auth = $google_auth;
    }

    /**
     * @SWG\Post(
     *     path="/admin/auth/login",
     *     summary="登录",
     *     tags={"Admin/auth"},
     *     description="",
     *     produces={"application/json"},
     *     @SWG\Parameter(
     *         name="username",
     *         in="query",
     *         description="手机号",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Parameter(
     *         name="password",
     *         in="query",
     *         description="密码",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="code=0：操作成功；其他操作失败，message中有错误信息"
     *     )
     * )
     */
    public function login()
    {
        try {
            $requestData = request()->all();

            $credentials = [
                'username'   => $requestData['username'],
                'password' => encode_pwd($requestData['password'])
            ];
            if ($token = $this->adminService->attempt($credentials)) {

                return $this->ajaxReturn(0, '', ['token' => $token]);
            } else {
                return $this->ajaxReturn(1002, '用户名或密码错误');
            }
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * @SWG\Post(
     *     path="/admin/auth/register",
     *     summary="注册",
     *     tags={"Admin/auth"},
     *     description="",
     *     produces={"application/json"},
     *     @SWG\Parameter(
     *         name="mobile",
     *         in="query",
     *         description="手机号",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Parameter(
     *         name="password",
     *         in="query",
     *         description="密码",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Parameter(
     *         name="username",
     *         in="query",
     *         description="用户名",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Parameter(
     *         name="email",
     *         in="query",
     *         description="邮箱",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="code=0：操作成功；其他操作失败，message中有错误信息"
     *     )
     * )
     */
    public function register()
    {
        try {
            $mobile = $this->input('mobile');
            if (!Validate::isMobile($mobile)) {
                return $this->ajaxReturn(1002, '手机号错误');
            }

            $password = $this->input('password');
            if (!Validate::isLenBetween($password, 6, 20)) {
                return $this->ajaxReturn(1002, '密码长度为6-20');
            }

            $email = $this->input('email');
            if (!Validate::isEmail($email)) {
                return $this->ajaxReturn(1002, '手机号错误');
            }

            $username = $this->input('username');
            if (empty($username)) {
                return $this->ajaxReturn(1002, '用户名错误');
            }

            $postData = [
                'mobile'   => $mobile,
                'password' => bcrypt(encode_pwd($password)),
                'email'    => $email,
                'username' => $username
            ];
            if ($uuid = $this->adminService->register($postData)) {
                return $this->ajaxReturn(0, '', (string)$uuid);
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * @SWG\Post(
     *     path="/admin/auth/me",
     *     summary="获取用户信息",
     *     tags={"Admin/auth"},
     *     description="",
     *     produces={"application/json"},
     *     security={{"Bearer":{}}},
     *     @SWG\Response(
     *         response=200,
     *         description="code=0：操作成功；其他操作失败，message中有错误信息"
     *     )
     * )
     */
    public function me()
    {
        try {
            $admin = $this->adminService->member();

            return $this->ajaxReturn(0, '', $admin);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * @SWG\Post(
     *     path="/admin/auth/refresh",
     *     summary="刷新token",
     *     tags={"Admin/auth"},
     *     description="",
     *     produces={"application/json"},
     *     security={{"Bearer":{}}},
     *     @SWG\Response(
     *         response=200,
     *         description="code=0：操作成功；其他操作失败，message中有错误信息"
     *     )
     * )
     */
    public function refresh()
    {
        try {
            $refresh = $this->adminService->refresh();

            return $this->ajaxReturn(0, '', $refresh);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * @SWG\Post(
     *     path="/admin/auth/logout",
     *     summary="退出",
     *     tags={"Admin/auth"},
     *     description="",
     *     produces={"application/json"},
     *     security={{"Bearer":{}}},
     *     @SWG\Response(
     *         response=200,
     *         description="code=0：操作成功；其他操作失败，message中有错误信息"
     *     )
     * )
     */
    public function logout()
    {
        try {
            $this->adminService->logout();

            return $this->ajaxReturn();
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

}
